package main

import "encoding/json"
import "fmt"
import "io/ioutil"

type AppConfig struct {
	Dbtype       string `json:"dbtype"`
	Dburl        string `json:"dburl"`
	CoapPort     uint16 `json:"coap_port"`
	WebPort      uint16 `json:"web_port"`
	LoggingLevel string `json:"logging"`
}

var global_config *AppConfig

func defaultConfig() {
	if global_config == nil {
		global_config = &AppConfig{
			Dbtype:       "sqlite3",
			Dburl:        "./uenb.db",
			CoapPort:     5683,
			WebPort:      8080,
			LoggingLevel: "error",
		}
	}
}

func readConfig(filename string) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println("open config file failed, using default.")
		defaultConfig()
		return
	}

	if global_config == nil {
		global_config = &AppConfig{}
	}
	json.Unmarshal(data, global_config)
}

func GlobalConfig() *AppConfig {
	return global_config
}
